package code14JDBC;

import com.mysql.cj.x.protobuf.MysqlxCrud;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 黄衡熙
 * @version 1.0
 */
public class Test03_单表操作 {
    public static void main(String[] args) {
//        User user = new User("jack", "222222", "13612369889", "江苏-苏州");
//        addUser(user);
        User user = new User("admin", "123456", "13612369888", "江苏-南京");
        User user2 = new User("alex", "222222", "13612369887", "江苏-徐州");
        User user3 = new User("lucy", "333333", "13612369886", "江苏-盐城");
        User user4 = new User("lily", "444444", "13612369885", "江苏-常州");
        User user5 = new User("alice", "555555", "13612369849", "江苏-连云港");
        User user6 = new User("john", "123456", "13612369888", "江苏-南京");
        User user7 = new User("tom", "123456", "13612369888", "江苏-南京");
        User user8 = new User("jack", "111111", "13612369888", "江苏-南京");
        addUser(user);
        addUser(user2);
        addUser(user3);
        addUser(user4);
        addUser(user5);
        addUser(user6);
        addUser(user7);
        addUser(user8);

       /* List<User> users = selectAll();
        for (User user : users) {
            System.out.println(user);
        }*/
    }


    public static void addUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_user(username,password,phone,address) ")
                    .append(" values(?,?,?,?) ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setString(4, user.getAddress());
            ps.executeUpdate();
            System.out.println("提示:恭喜您,添加成功");
        } catch (SQLException e) {
            throw new DataAccessException("数据访问异常");
        } finally {
            // 对于不需要释放的资源,其值直接给null即可
            JDBCUtil.close(conn, ps, null);
        }
    }

    public static List<User> selectAll() {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        List<User> users = new ArrayList<>();
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .toString();
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while (rs.next()) {
                User user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
                users.add(user);
            }
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException("数据访问异常");
        }
        return users;
    }

    //修改
    public static void updateUser(User user) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" update t_user ")
                    .append(" set ")
                    .append(" username=?, ")
                    .append(" password=?, ")
                    .append(" phone=?, ")
                    .append("address=? ")
                    .append(" where id=?, ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getPhone());
            ps.setString(4, user.getAddress());
            ps.setInt(5, user.getId());
            ps.executeUpdate();
            System.out.println("恭喜您修改成功");
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException("数据访问异常");
        } finally {
            JDBCUtil.close(conn, ps, null);
        }

    }

    public static void deleteById(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql=new StringBuffer()
                    .append("drop t_user ")
                    .append(" where id=? ")
                    .toString();
            ps=conn.prepareStatement(sql);
            System.out.println("恭喜您删除成功");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            JDBCUtil.close(conn,ps,null);
        }
    }

    public static User selectById(Integer id) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        User user = null;
        try{
            conn = JDBCUtil.getConnection();
            String sql=new StringBuffer()
                    .append(" select id,username,password,phone,address ")
                    .append(" from t_user ")
                    .append(" where id=? ")
                    .toString();
            ps=conn.prepareStatement(sql);
            ps.setInt(1, id);
            rs=ps.executeQuery();
            while (rs.next()) {
                user = new User();
                user.setId(rs.getInt("id"));
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                user.setPhone(rs.getString("phone"));
                user.setAddress(rs.getString("address"));
            }
            return user;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            JDBCUtil.close(conn,ps,null);
        }

    }

}
